package com.gnerv.battle.boot.project.guard.controller;

import com.gnerv.battle.boot.project.guard.business.MonitorNodeConfigBusiness;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "api/monitor/node/config")
public class MonitorNodeConfigController {

    private final MonitorNodeConfigBusiness monitorNodeConfigBusiness;

    @GetMapping(value = "{ukId}")
    public void selectByUkId(@PathVariable(value = "ukId") String ukId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String configFile = monitorNodeConfigBusiness.selectConfigFileByUkId(ukId, request);
        FileCopyUtils.copy(configFile.getBytes(StandardCharsets.UTF_8), response.getOutputStream());
    }

}
